package com.gykfbxxh.imovie.view.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.BaseActivity;
import com.gykfbxxh.imovie.bean.RanNumBean;
import com.gykfbxxh.imovie.bean.info.UserInfor;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.custom.net.ParamMap;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.manager.UserInforManager;
import com.gykfbxxh.imovie.utils.HttpUtils;

import static com.gykfbxxh.imovie.config.ActivityRequestCode.REQUEST_CODE_USERNAME;

public class UserInfoActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_username ,tv_phonenumber;
    private RelativeLayout rl_username , rl_updatepwd  ,rl_bindphone;
    private CheckBox cb_sex_man,cb_sex_woman;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.activity_user_info);
        setTitle("个人信息");

        initView();
        initData();
    }

    private void initView() {
        tv_username = (TextView)findViewById(R.id.tv_username);
        rl_username = (RelativeLayout)findViewById(R.id.rl_username);
        cb_sex_man = (CheckBox)findViewById(R.id.cb_sex_man);
        cb_sex_woman = (CheckBox)findViewById(R.id.cb_sex_woman);
        rl_updatepwd = (RelativeLayout)findViewById(R.id.rl_updatepwd);
        rl_bindphone = (RelativeLayout)findViewById(R.id.rl_bindphone);
        tv_phonenumber = (TextView)findViewById(R.id.tv_phonenumber);
        rl_username.setOnClickListener(this);
        cb_sex_man.setOnClickListener(this);
        cb_sex_woman.setOnClickListener(this);
        rl_updatepwd.setOnClickListener(this);
        rl_bindphone.setOnClickListener(this);
    }

    private void initData() {
        boolean login = UserInforManager.getInstance().isLogin();
        if(!login) {
            onBackPressed();
        }
        UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
        tv_username.setText(userInfor.getfUsername());

        if("男".equals(userInfor.getfSex())) {
            cb_sex_man.setChecked(true);
            cb_sex_woman.setChecked(false);
        }else {
            cb_sex_man.setChecked(false);
            cb_sex_woman.setChecked(true);
        }
        tv_phonenumber.setText(userInfor.getfPhone());
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.rl_username://修改用户昵称
                updateUserName();
                break;
            case R.id.cb_sex_man:
                switchSex("男");
                break;
            case R.id.cb_sex_woman:
                switchSex("女");
                break;
            case R.id.rl_updatepwd:
                Intent intent = new Intent(this, UpdatePwdActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_bindphone:
                Intent intent1 = new Intent(this, BindPhoneActivity.class);
                startActivityForResult(intent1,REQUEST_CODE_USERNAME);
                break;
        }
    }

    private void switchSex(final String sex) {
        ParamMap paramMap = new ParamMap();
        paramMap.put("sex" , sex);
        HttpUtils.addPostRequest(URIContant.UPDATEUSERINFOSEX, new TypeToken<RanNumBean>() {
        }.getType(), mPageName, paramMap, new HttpListener<RanNumBean>() {
            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText("修改失败");
            }

            @Override
            public void onSuccess(RanNumBean response) {
                if(response!=null&&response.noError()) {
                    UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
                    userInfor.setfSex(sex);
                    UserInforManager.getInstance().setUserInfor(userInfor);
                    ToastUtils.makeText("修改成功");
                    if("男".equals(sex)) {
                        cb_sex_man.setChecked(true);
                        cb_sex_woman.setChecked(false);
                    }else {
                        cb_sex_man.setChecked(false);
                        cb_sex_woman.setChecked(true);
                    }
                }else {
                    ToastUtils.makeText("修改失败");
                }
            }
        });
    }

    private void updateUserName() {
        Intent intent = new Intent(this, UpdateUserNameActivity.class);
        startActivityForResult(intent,REQUEST_CODE_USERNAME);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==REQUEST_CODE_USERNAME) {
            if(resultCode == RESULT_OK ) {
                initData();
            }
        }
    }
}
